package command.project;

import javax.xml.bind.annotation.XmlTransient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import model.User;
import command.StatechartCommand;
import controller.ApplicationController;

public class CreateUserCommand extends StatechartCommand {

	private Long projectID;
	private String userName;

	private User user;
	
	@XmlTransient
	private static final Logger log = LoggerFactory
			.getLogger(CreateUserCommand.class);

	public CreateUserCommand(Long projectID, String userName) {
		this.projectID = projectID;
		this.userName = userName;
	}

	@Override
	public void executeLocal(ApplicationController applicationController) {
		log.debug("Executing command : CreateUserCommand ...");

		user = applicationController.getUserFactory().createUser(userName);
		applicationController.getModel().getCurrentProject().getUsers()
				.add(user);
		log.debug("Executing command : CreateUserCommand DONE");

	}

	@Override
	public void executeRemote(ApplicationController applicationController) {
		log.debug("Executing remote command : CreateUserCommand ...");

		if (applicationController.getModel().getCurrentProject() != null
				&& applicationController.getModel().getCurrentProject().getId()
						.equals(projectID)) {
			log.debug("Executing remote command : CreateUserCommand (projet is open) ...");

			applicationController.getModel().getCurrentProject().getUsers()
					.add(user);
		} else {
			log.debug("Executing remote command : CreateUserCommand (projet is not open) ...");
		}
		log.debug("Executing remote command : CreateUserCommand DONE");
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
